<%@ include file="../import_page.jsp"%>
<%@ page import="java.util.Calendar"%>
<%!
	static int tryToInt(String str) {
		if(isEmptyString(str))
			return -1;
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException err) {
			
		}
		return -1;
	}

%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.staff)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	AssignmentCollector ac = null;
	boolean inputErr = false;
	
	//Create control of assignment
	AssignmentsControl aCon = new AssignmentsControl();
	//try to catch Assignment ID input
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	if(!isEmptyString(acIdStr)) {
		try {
			int acId = Integer.parseInt(acIdStr);
			if(acId < 1)
				ac = new AssignmentCollector(acId , selectUnit.getId());
			else
			{
				//get the assignment from database
				ac = aCon.getAssignment(acId);
				//Check the unit of assignment is match the selected unit
				if(ac.getUnitID().indexOf(selectUnit.getId()) < 0)
					throw new Exception("Select Unit do not match the Assignment collector Unit");
			}
		} catch (Exception err) {
			errMsg = err.toString();
		}
	}
	else {
		//if can't catch assignment return error message
		errMsg = "Missing Assignment ID input";
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Confirm modify Assignment</title>
</head>
<body>
	<%if(isEmptyString(errMsg)) { %>
	<table width="100%" border="1">
		<tr>
			<td colspan="2"><p><%= ac.getId() < 1 ? "Create New Assignment Collector" : "Modify Assignment Collector" %></p>
			</td>
		</tr>
		<tr>
			<td bgcolor="#CCCCCC"><p>Unit Selected</p>
			</td>
			<td bgcolor="#CCCCCC"><%= selectUnit.getName() %> (<%=selectUnit.getId() %>)</td>
		</tr>
		<tr>
			<td>Assignment Name<%= ac.getId() < 1 ? "" : " (ID)"  %></td>
			<%
//get assignment name
String ac_name = request.getParameter(PAR_ASSIGNMENT_NAME);
if(isEmptyString(ac_name) || ac_name.length() > AssignmentCollector.NAME_MAX_LENGTH) {
	inputErr = true;
%>
			<td>Incorrect Assignment Name</td>
			<%} else { 
	ac.setName(ac_name);
%>
			<td><%= ac.getName() %><%= ac.getId() < -1 ? "" : "("+ ac.getId()+ ")" %></td>
			<%} %>
		</tr>
		<tr>
			<td bgcolor="#CCCCCC">DeadLine</td>
			<td bgcolor="#CCCCCC">
				<%
int year = tryToInt(request.getParameter("year"));
int month =  tryToInt(request.getParameter("month")) - 1;
int date = tryToInt(request.getParameter("date"));
int hour = tryToInt(request.getParameter("hour"));
int minute = tryToInt(request.getParameter("minute"));
int second = tryToInt(request.getParameter("second"));
try {
	//Calendar for gen data object
	Calendar targetTime = Calendar.getInstance();
	targetTime.set(year, month, date, hour, minute, second);
	ac.setDeadline(targetTime.getTime());
	out.print(formatDateTime(ac.getDeadline()));
} catch (ArrayIndexOutOfBoundsException err) {
	out.print("Incorect Datetime format");
	inputErr = true;
} catch (Exception err) {
	out.print(err.toString());
	inputErr = true;
}
%>
			</td>
		</tr>
		<tr>
			<td>Assignment Status</td>
			<td><p>
					Submission:
					<%
String isSub = request.getParameter("isSubmission");
if(isEmptyString(isSub)) {
	inputErr = true;
	out.print("Incorrect input");
} else {
	ac.setSubmission(isSub.equalsIgnoreCase("Open"));
	out.print(ac.isSubmission() ? "Open" : "Close");
}
%>
				</p>
				<p>
					Marking:
					<%
String isMark = request.getParameter("isMarking");
if(isEmptyString(isMark)) {
	out.print("Incorrect input");
	inputErr = true;
} else {
	ac.setMarking(isMark.equalsIgnoreCase("Open"));
	out.print(ac.isMarking() ? "Open" : "Close");
}
%>
				</p>
			</td>
		</tr>
		<tr>
			<td bgcolor="#CCCCCC">Assignment Type</td>
			<td bgcolor="#CCCCCC">
				<%
int groupSize = tryToInt(request.getParameter("group_size"));
if(groupSize < 1) {
	inputErr = true;
	out.print("Incorrect input");
} else if(groupSize > 1) {
	ac.setGroupSize(groupSize);
	out.print("Group ("+ groupSize+ ")");
} else {
	ac.setGroupSize(groupSize);
	out.print("Individual");
}
%>
			</td>
		</tr>
		<tr>
			<td bgcolor="#CCCCCC">Full Mark</td>
			<td bgcolor="#CCCCCC">
				<%
try {
	BigDecimal fullMark = new BigDecimal(request.getParameter("full_mark"));
	ac.setFullMark(fullMark);
	out.print(fullMark.toString());
} catch (Exception err) {
	inputErr = true;
	out.print("Incorrect full mark input");
}
%>
			</td>
		</tr>
		<tr>
			<td>Description</td>
			<td>
				<%
String info = request.getParameter("info");
if(info == null)
	info = "";
ac.setInfo(info);
%> <textarea name="info" cols="50" rows="5" readonly="readonly"><%=ac.getInfo() %></textarea>
			</td>
		</tr>
		<tr>
			<td bgcolor="#CCCCCC" colspan="2">
				<%if(inputErr) { %> Incorrect Assignment collector <a
				href="javascript: history.go(-1);">Return</a> <%
} else {
	try {
		if(ac.getId() < 1) {
			//Create a New Assignment collector
			aCon.createNewAssignment(ac);
		} else {
			//Modify the assignment collector
			aCon.updateAssignment(ac);
		}
		out.print("Assignment collector update complete");
	} catch (Exception err) {
		out.print("Error in update Assignment collector: <br />\r\n"+ err.toString());
	}
	aCon.closeDBConnection();
}
%>
			</td>
		</tr>
	</table>
	<%
} else {
	aCon.closeDBConnection();
%>
	<p><%=errMsg %></p>
	<%} %>
</body>
</html>